package org.news.web.controller;

import java.util.List;

import com.dtflys.forest.annotation.Get;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.FavoritesVo;
import org.news.web.domain.bo.FavoritesBo;
import org.news.web.service.IFavoritesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 收藏功能
 *
 * @author Gu Ci
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/favorites")
public class FavoritesController extends BaseController {

    private final IFavoritesService favoritesService;

    /**
     * 查询收藏功能列表
     */
    @SaCheckPermission("web:favorites:list")
    @GetMapping("/list")
    public TableDataInfo<FavoritesVo> list(FavoritesBo bo, PageQuery pageQuery) {
        return favoritesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出收藏功能列表
     */
    @SaCheckPermission("web:favorites:export")
    @Log(title = "收藏功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FavoritesBo bo, HttpServletResponse response) {
        List<FavoritesVo> list = favoritesService.queryList(bo);
        ExcelUtil.exportExcel(list, "收藏功能", FavoritesVo.class, response);
    }

    /**
     * 获取收藏功能详细信息
     *
     * @param favoriteId 主键
     */
    @SaCheckPermission("web:favorites:query")
    @GetMapping("/{favoriteId}")
    public R<FavoritesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long favoriteId) {
        return R.ok(favoritesService.queryById(favoriteId));
    }

    /**
     * 新增收藏功能
     */
    @SaCheckPermission("web:favorites:add")
    @Log(title = "收藏功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FavoritesBo bo) {
        return toAjax(favoritesService.insertByBo(bo));
    }

    /**
     * 修改收藏功能
     */
    @SaCheckPermission("web:favorites:edit")
    @Log(title = "收藏功能", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FavoritesBo bo) {
        return toAjax(favoritesService.updateByBo(bo));
    }

    /**
     * 删除收藏功能
     *
     * @param favoriteIds 主键串
     */
    @SaCheckPermission("web:favorites:remove")
    @Log(title = "收藏功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{favoriteIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] favoriteIds) {
        return toAjax(favoritesService.deleteWithValidByIds(List.of(favoriteIds), true));
    }


    /**
     * 添加收藏
     */
    @GetMapping("/favo/{userId}/{newsId}")
    public R<Void> favo(@PathVariable("userId") Long userId,@PathVariable("newsId") Long newsId){
        return toAjax(favoritesService.favo(userId,newsId));
    }


    /**
     * 取消收藏
     */
    @GetMapping("/nofavo/{userId}/{newsId}")
    public R<Void> nofavo(@PathVariable("userId") Long userId,@PathVariable("newsId") Long newsId){
        return toAjax(favoritesService.nofavo(userId,newsId));
    }
}
